package com.example.demo.controller;

import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@RestController
public class LoggerController {
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    @GetMapping("/logger")
    public String get(){
        System.out.println("sout");
        logger.trace("trace");
        logger.debug("debug");
        logger.info("info");
        logger.warn("warn");
        logger.error("error");
        return "example/hello";
    }
    @GetMapping("logRequest")
    public String logRequest(){
        ServletRequestAttributes requestAttributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=requestAttributes.getRequest();
        logger.info(request.getMethod()+" "+request.getRequestURI());
        return "example/hello";
    }
    @GetMapping("/logRequestTime")
    public String logRequestTime() throws InterruptedException {
        Long startTime =System.currentTimeMillis();
        Thread.sleep(1000);
        logger.info("cost:"+(System.currentTimeMillis()-startTime));
        return "example/hello";
    }
}
